<?php
class Util_Activity{
    public static function getActivityList($limit,$offset,$uid){
        list($counts,$data) = self::getActivity($limit,$offset,$uid);
        $list = array();
        foreach($data as $v){
            $where = array(
                'key'=>'type',
                'cond'=>'=',
                'val'=>'ACTIVITY'
            );
            $tmp = User_TouristComments::tourist_track_check_out('track_id',$v['id'],$where);
            $avg = User_TouristComments::tourist_activity_score('track_id',$v['id'],$where);
            $count = $tmp->count();

            $v['avg'] = sprintf("%.2f",$avg);
            $v['count'] = $count;
            $v['created'] = date('Y.m.d H:i',$v['created']);
            $list[] = $v;
        }
        return array($counts,$list);
    }

    public static function getActivityDetail($uid,$id){
        list($_,$data) =self::getActivity(NULL,NULL,$uid,$id);
        $list = array();
        foreach($data as $v){
            $where = array(
                'key'=>'type',
                'cond'=>'=',
                'val'=>'ACTIVITY'
            );
            $avg = User_TouristComments::tourist_activity_score('track_id',$v['id'],$where);
            $query = User_TouristComments::tourist_track_check_out('track_id',$v['id'],$where);
            $count = $query->count();
            $comments = $query->as_array();
            foreach($comments as &$cv){
                $info = User_UserInfo::user_info($cv['uid'])->current();
                $account = User_Account::account_checkout('uid',$cv['uid'])->current();
//                var_dump($account);
                $cv['userIcon'] = $info['userIcon'];
                $cv['nickname'] = $account['userName'];
                $cv['created'] = date("Y.m.d H:i:s",$cv['created']);
                $cv['tourist_pic'] = explode(';',trim($cv['tourist_pic'],';'));
            }

            $sc3 = User_TouristComments::activity_comments_check($v['id'],array(
                    'key'=>'track_score',
                    'cond'=>'<=',
                    'val'=>3)
            )->count();

            $sc5 = User_TouristComments::activity_comments_check($v['id'],array(
                    'key'=>'track_score',
                    'cond'=>'>=',
                    'val'=>4)
            )->count();

            $v['detail'] = $comments;
            $v['avg'] = sprintf("%.2f",$avg);
            $v['count'] = $count;
            $v['sc3'] = $sc3;
            $v['sc4'] = $count-$sc3-$sc5;
            $v['sc5'] = $sc5;
            $v['startDay'] = date("Y.m.d",strtotime($v['startDay']));
            $v['stopDay'] = date("Y.m.d",strtotime($v['stopDay']));
            $list = $v;
        }

        return $list;
    }

    public static function getActivity($limit,$offset,$uid,$id = -1){
        $query = DB::select('id','name','created','startDay','stopDay')
            ->from('mod_activity')
            ->where('status','=','VALID')
            ->where('uid','=',$uid);

        if($id != -1){
            $query = $query->where('id','=',$id);
        }
        $counts = $query->execute()->count();
        if($limit != "" || $offset != ""){
            $query = $query ->offset($offset)
                ->limit($limit);
        }
        $list = $query->execute()->as_array();
        return array($counts,$list);

    }
}